package hang.xipujewelWeb.web.account;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import net.esoar.modules.persistence.FilterUtil;
import net.esoar.modules.utils.WebUtil;
import net.esoar.modules.web.BaseController;
import net.esoar.modules.web.pojo.JPage;
import net.esoar.modules.web.pojo.WebPojo;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import hang.xipujewelWeb.entity.account.Role;
import hang.xipujewelWeb.pojo.authority.Permission;
import hang.xipujewelWeb.pojo.authority.WebViews;
import hang.xipujewelWeb.service.account.RoleManager;

/**
 * 角色管理
 * 
 * @author qizai
 * 
 */
@Controller
@RequestMapping(WebViews.SYSTEM_BASE_ROLE)
public class RoleController extends BaseController<Role> {
	// 权限及路径
	private final static String VIEW = WebViews.R_ROLE_VIEW;
	private final static String EDIT = WebViews.R_ROLE_EDIT;
	private final static String MODEL = WebViews.MODEL_ROLE;
	private final static String BASE_MAPPING = WebViews.SYSTEM_BASE_ROLE;
	//
	@Autowired
	private RoleManager roleManager;
	/**
	 * 分页浏览
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequiresPermissions(VIEW)
	@RequestMapping(value = LIST)
	public String list(Model model, ServletRequest request) {
		Map<String, Object> searchParams = FilterUtil.getFilterParameters(request);
		jPage = FilterUtil.buildPage(request, jPage);
		if (!jPage.isOrderBySetted()) {
			jPage.setOrderBy("id");
			jPage.setOrder(JPage.ASC);
		}
		jPage = roleManager.search(searchParams, jPage);
		return getListView();
	}
	/**
	 * 新增或查看
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions(VIEW)
	@RequestMapping(value = INPUT, method = RequestMethod.GET)
	public String input(@PathVariable(ID) Long id, Model model) {
		Map<String, List<Permission>> permissionListByType = Permission.getListMap();
		Role role = roleManager.get(id);
		model.addAttribute(MODEL, role);
		model.addAttribute("checkedPermission", role.getPermissions());
		model.addAttribute("permissionListByType", permissionListByType);

		return getInputView();
	}
	/**
	 * 保存/更新
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions(EDIT)
	@RequestMapping(value = SAVE, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public WebPojo save(@Valid @ModelAttribute(MODEL) Role model) {

		roleManager.save(model);

		return WebUtil.getCloseCurrent(getTabId());
	}
	/**
	 * 删除固定id的记录
	 * 
	 * @param id
	 *            主键
	 * @return
	 */
	@RequiresPermissions(EDIT)
	@RequestMapping(value = DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public WebPojo delete(@PathVariable(ID) Long id) {
		roleManager.delete(id);

		return WebUtil.getNavTabAjaxDone(getTabId());
	}

	@ModelAttribute(MODEL)
	public Role getModel(@RequestParam(value = ID, required = false) Long id) {
		if (id != null) {
			return roleManager.get(id);
		}
		return null;
	}
	//
	@Override
	public String baseMapping() {
		return BASE_MAPPING;
	}
	@Override
	public String getModelName() {
		return MODEL;
	}
}
